#define  _CRT_SECURE_NO_WARNINGS
#include "Stack.h"


void CheckCapacity(ST* phead)
{
	assert(phead);
	if (phead->capacity == phead->top)
	{
		int newcapacity = phead->capacity == 0 ? 4 : 2 * phead->capacity;
		STDataType* tmp = (STDataType*)realloc(phead->arr, newcapacity * sizeof(STDataType));
		assert(tmp);
		phead->arr = tmp;
		phead->capacity = newcapacity;
	}
}


void InitStack(ST* phead)
{
	assert(phead);
	phead->arr = NULL;
	phead->capacity = phead->top = 0;
}

void StackPush(ST* phead, STDataType x)
{
	assert(phead);
	CheckCapacity(phead);
	phead->arr[phead->top] = x;
	phead->top++;
}

bool STEmpty(ST* phead)
{
	assert(phead);
	return phead->top == 0;
}

void StackPop(ST* phead)
{
	assert(phead);
	assert(!STEmpty(phead));
	phead->top--;
}

STDataType StackTop(ST* phead)
{
	assert(phead);
	assert(!STEmpty(phead));
	return phead->arr[phead->top - 1];
}

int StackSize(ST* phead)
{
	assert(phead);

	return phead->top;
}

void StackDestroy(ST* phead)
{
	assert(phead);
	assert(phead->arr);
	if (phead->arr)
		free(phead->arr);
	phead->arr = NULL;
	phead->capacity = phead->top = 0;
}
